package com.msmall.community.convene.action.html;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.util.ConstantUrl;
import com.msmall.community.convene.model.Convene;
import com.msmall.community.convene.service.ConveneManager;

public class ConveneAction extends HTMLBaseAction {

	private static final long serialVersionUID = 4690242169107736055L;
	
	protected static final String SESSION_CONVENE_KEY = "SESSION_CONVENE_KEY";
	
	private Long conveneId;
	
	private String conveneIds;
	
	private Convene convene;
	
	private ConveneManager conveneManager;
	
	/**
	 * 进入召集令首页，查看所有召集令
	 */
	public String execute(){
		page.setClazz(Convene.class);
		page.setOrderBy("createTime");
		page.setOrder("DESC");
		page.setPageSize(20);
		conveneManager.getPagination(page);
		page.setUrl(ConstantUrl.HTML_CONVENE);
		return SUCCESS;
	}
	public String myConvene(){
		try {
			page.setPageSize(20);
			conveneManager.getMyConvenes(page, getUserid());
			page.setUrl(ConstantUrl.HTML_CONVENE_MY);
		} catch (Exception e) {
		}
		return SUCCESS;
	}
	
	/**
	 * 保存
	 */
	public String save(){
		if(!getIsLogin()){
			message = getText("common.authentication.anonymous");
			setToSession(SESSION_CONVENE_KEY, convene);
			setToSession(SESSION_MESSAGE_KEY, message);
			return INPUT;
		}
		int validtitle = conveneManager.validateConveneTitle(convene.getTitle());
		if(validtitle == 2){
			setToSession(SESSION_CONVENE_KEY, convene);
			message = getText("html.convene.create.title.length.short");
			setToSession(SESSION_MESSAGE_KEY, message);
			return INPUT;
		}
		if(validtitle == 3){
			setToSession(SESSION_CONVENE_KEY, convene);
			message = getText("html.convene.create.title.length.long");
			setToSession(SESSION_MESSAGE_KEY, message);
			return INPUT;
		}
		if(validtitle == 4){
			setToSession(SESSION_CONVENE_KEY, convene);
			message = getText("html.convene.create.title.illegal");
			setToSession(SESSION_MESSAGE_KEY, message);
			return INPUT;
		}
		int validcontent = conveneManager.validateConveneContent(convene.getContent());
		if(validcontent == 2){
			setToSession(SESSION_CONVENE_KEY, convene);
			message = getText("html.convene.create.content.length.short");
			setToSession(SESSION_MESSAGE_KEY, message);
			return INPUT;
		}
		if(validcontent == 3){
			setToSession(SESSION_CONVENE_KEY, convene);
			message = getText("html.convene.create.content.length.long");
			setToSession(SESSION_MESSAGE_KEY, message);
			return INPUT;
		}
		if(validcontent == 4){
			setToSession(SESSION_CONVENE_KEY, convene);
			message = getText("html.convene.create.content.illegal");
			setToSession(SESSION_MESSAGE_KEY, message);
			return INPUT;
		}
		try {
			conveneManager.saveConvene(convene, getUserid());
		} catch (Exception e) {
		}
		removeFromSession(SESSION_CONVENE_KEY); //清空
		removeFromSession(SESSION_MESSAGE_KEY); //清空
		return SUCCESS;
	}
	/**
	 * 创建
	 */
	public String create(){
		if(message == null){
			message = (String) getFromSession(SESSION_MESSAGE_KEY);
			if(message!= null && getText("common.authentication.anonymous").equalsIgnoreCase(message)){
				if(getIsLogin()){
					message = null;
					removeFromSession(SESSION_MESSAGE_KEY);
				}
			}
		}
		conveneId = null;
		if(convene == null){
			convene = (Convene) getFromSession(SESSION_CONVENE_KEY);
		}
		return SUCCESS;
	}
	/**
	 * 更新
	 */
	public String update(){
		convene = (Convene) conveneManager.get(Convene.class, conveneId);
		return SUCCESS;
	}
	public String delete(){
		try{
			conveneManager.removeConvene(conveneIds, getUserid());
		}catch(Exception e){
		}
		return SUCCESS;
	}
	/**
	 * 查看
	 */
	public String view(){
		convene = (Convene) conveneManager.get(Convene.class, conveneId);
		return SUCCESS;
	}
	
	public Long getConveneId() {
		return conveneId;
	}
	public void setConveneId(Long conveneId) {
		this.conveneId = conveneId;
	}
	public String getConveneIds() {
		return conveneIds;
	}
	public void setConveneIds(String conveneIds) {
		this.conveneIds = conveneIds;
	}
	public Convene getConvene() {
		return convene;
	}
	public void setConvene(Convene convene) {
		this.convene = convene;
	}
	public void setConveneManager(ConveneManager conveneManager) {
		this.conveneManager = conveneManager;
	}
	
}
